package action;

import java.awt.event.TextEvent;
import java.io.*;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;

import lombok.Data;
import org.springframework.util.DigestUtils;
import pojo.SysUsers;
import service.SysUsersService;
import util.GsonUtil;
import util.PageInfo;

@Data
public class SysUsersAction {
	@Autowired
	private SysUsersService service;
	private SysUsers sysUsers;
    int pageSize=1;  //每页多少条
    int pageNum=1; //第几页
    private File file;
    private String fileFileName;  //自动获得原文件名
    private String fileName; //新文件名


	public String queryLogin() {
        String pwd = DigestUtils.md5DigestAsHex(("1234"+sysUsers.getPassword()).getBytes());
        sysUsers.setPassword(pwd);
        List<SysUsers> list=service.queryLogin(sysUsers);
        ActionContext.getContext().getSession().put("list", list);
        if(list.get(0).getSysRoles().getId()==1){
            return "querymenu";
        }else if(list.get(0).getSysRoles().getId()==2){
            return "querymenu2";
        }else{
            return "querymenu3";
        }

	}
	public void queryByName() throws Exception {
        String pwd = DigestUtils.md5DigestAsHex(("1234"+sysUsers.getPassword()).getBytes());
        sysUsers.setPassword(pwd);
        List<SysUsers> list=service.queryLogin(sysUsers);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println(JSON.toJSONString(list));
		out.close();
	}

    public void queryall() throws Exception {
        PageInfo pageInfo=  service.queryByLike2(sysUsers, pageNum, pageSize);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(
                "["+GsonUtil.toJson(pageInfo)+","+GsonUtil.toJson(pageInfo.getList())+"]"
        );
        out.close();
    }
    public void queryAll() throws Exception{
        List<SysUsers> sele = service.queryAll();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(GsonUtil.toJson(sele));
        out.close();
    }
    public String save() throws  Exception{

        //获得tomcat 的路径
        String path = ServletActionContext.getServletContext().getRealPath("/");
        fileName=System.currentTimeMillis()+
                fileFileName.substring(fileFileName.lastIndexOf("."));
        //写
        FileOutputStream fos=new FileOutputStream(path+"/images/"+fileName);
        //读
        FileInputStream fis=new FileInputStream(file);

        byte [] b=new byte[1024*50];

        int len=0;
        while((len=fis.read(b))>0){
            fos.write(b,0,len);
        }
        sysUsers.setHeadUrl(fileName);
        service.userAdd(sysUsers);
        fos.close();
        fis.close();

	    return "success";
    }
}
